import { Audio } from 'expo-av';

export const soundPlayer = {
  longPressOnSound: null as Audio.Sound | null,
  longPressOffSound: null as Audio.Sound | null,

  async initsound() {
    try {
      console.log('Initializing sound player');
      await Audio.setAudioModeAsync({
        playsInSilentModeIOS: true,
        staysActiveInBackground: false,
        shouldDuckAndroid: true,
      });

      const soundConfig = {
        shouldPlay: false,
        volume: 1.0,
        rate: 1.0,
        shouldCorrectPitch: true,
        progressUpdateIntervalMillis: 50,
        positionMillis: 0,
        isMuted: false,
      };

      const { sound: soundOn } = await Audio.Sound.createAsync(
        require('../../assets/audio/longpresson.wav'),
        soundConfig
      );
      const { sound: soundOff } = await Audio.Sound.createAsync(
        require('../../assets/audio/longpressoff.wav'),
        soundConfig
      );
      
      await soundOn.setVolumeAsync(1.0);
      await soundOff.setVolumeAsync(1.0);
      
      this.longPressOnSound = soundOn;
      this.longPressOffSound = soundOff;
      console.log('Sound player initialized successfully');
    } catch (error) {
      console.error('Failed to load sounds:', error);
    }
  },

  async playLongPressOn() {
    try {
      console.log('Playing long press ON sound');
      if (this.longPressOnSound) {
        await this.longPressOnSound.setPositionAsync(0);
        const status = await this.longPressOnSound.getStatusAsync();
        console.log('Long press ON sound status:', status);
        
        await this.longPressOnSound.playAsync();
        return new Promise<void>((resolve) => {
          this.longPressOnSound?.setOnPlaybackStatusUpdate((status) => {
            if (status.isLoaded && status.didJustFinish) {
              console.log('Long press ON sound finished playing');
              resolve();
            }
          });
        });
      } else {
        console.log('Long press ON sound not loaded');
      }
    } catch (error) {
      console.error('Failed to play long press on sound:', error);
    }
  },

  async playLongPressOff() {
    try {
      console.log('Playing long press OFF sound');
      if (this.longPressOffSound) {
        await this.longPressOffSound.setPositionAsync(0);
        const status = await this.longPressOffSound.getStatusAsync();
        console.log('Long press OFF sound status:', status);
        
        await this.longPressOffSound.playAsync();
        return new Promise<void>((resolve) => {
          this.longPressOffSound?.setOnPlaybackStatusUpdate((status) => {
            if (status.isLoaded && status.didJustFinish) {
              console.log('Long press OFF sound finished playing');
              resolve();
            }
          });
        });
      } else {
        console.log('Long press OFF sound not loaded');
      }
    } catch (error) {
      console.error('Failed to play long press off sound:', error);
    }
  },

  async play(audioSource: any) {
    try {
      if (this.longPressOnSound) {
        await this.longPressOnSound.unloadAsync();
      }
      if (this.longPressOffSound) {
        await this.longPressOffSound.unloadAsync();
      }
      const { sound } = await Audio.Sound.createAsync(audioSource);
      await sound.playAsync();
    } catch (error) {
      console.error('Error playing sound:', error);
    }
  },

  async unload() {
    try {
      if (this.longPressOnSound) {
        await this.longPressOnSound.unloadAsync();
      }
      if (this.longPressOffSound) {
        await this.longPressOffSound.unloadAsync();
      }
      console.log('Sounds unloaded successfully');
    } catch (error) {
      console.error('Failed to unload sounds:', error);
    }
  }
}; 